﻿package net.ojbk.jiaofu.service;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.io.File;

import net.ojbk.dao.UserDataDao;
import net.ojbk.database.DatabaseConnection;
import net.ojbk.jiaofu.dao.SyTaskDao;
import net.ojbk.jiaofu.po.*;
import net.ojbk.po.UserData;
import net.ojbk.jiaofu.dao.SyTaskSubmitDao;
import net.ojbk.util.StringTool;
import net.ojbk.util.WebPathTool;

public class TaskService {
	
	public static int getCountByTaskId(String taskId) {
		DatabaseConnection dbcon = new DatabaseConnection();
		SyTaskSubmitDao stsd=new SyTaskSubmitDao(dbcon);
		int count=stsd.getCountByTaskId(taskId);
		return count;
	}
	public static SyTask getTask(String taskId) {
		DatabaseConnection dbcon = new DatabaseConnection();
		SyTask task = new SyTask();
		task.setTaskId(taskId);
		new SyTaskDao(dbcon).queryOneRecord(task);
		dbcon.close();
		return task;
	}
	
	public static int getTaskType(String taskId) {
		DatabaseConnection dbcn=new DatabaseConnection();
		SyTaskDao taskDao = new SyTaskDao(dbcn);
		SyTask task = new SyTask();
		task.setTaskId(taskId);
		taskDao.queryOneRecord(task);
		dbcn.close();
		return task.getTaskType();
	}
	public static String getZipDirPath(String userId, String matchId) {
		return WebPathTool.getTaskDataPath() + userId + File.separator
				+ matchId + File.separator
				+ "treasury" + File.separator;
	}
	public static String getTaskRootPath(String userId, String matchId) {
		return WebPathTool.getTaskDataPath() + userId + File.separator
				+ matchId + File.separator;
	}
	public static String getZipPath(String userId, String matchId) {
		return WebPathTool.getTaskDataPath() + userId + File.separator
				+ matchId + File.separator
				+ "任务资料合集.zip";
	}
	
	public static int[] getSubmitNum(String taskId) {
		SyTask st=new SyTask();
		st.setTaskId(taskId);
		DatabaseConnection dbcon=new DatabaseConnection();
		SyTaskDao std=new SyTaskDao(dbcon);
		boolean flag1=std.queryOneRecord(st);
		System.out.println(flag1);
		Date beginTime=st.getBeginTime();
		Date endTime=st.getEndTime();
		System.out.println(beginTime);
		System.out.println(endTime);
		
		final int len=(int)((endTime.getTime()-beginTime.getTime())/18000000)+2;
		int[] SubmitNumEveryHour=new int[len];
		SyTaskSubmitDao stsd=new SyTaskSubmitDao(dbcon);
		SyTaskSubmit[] stsList=stsd.queryAllRecodByTaskId(taskId);
		for(int i=0;i<stsList.length;i++)
		{
			int flag=(int)((stsList[i].getSubmitTime().getTime()-beginTime.getTime())/18000000+1);
			System.out.println(flag);
			if(stsList[i].getStatus()==1)
				SubmitNumEveryHour[flag]++;
		}
		return SubmitNumEveryHour;
	}
	public static String[] getSubmitTime(String taskId) {
		SyTask st=new SyTask();
		st.setTaskId(taskId);
		DatabaseConnection dbcon=new DatabaseConnection();
		SyTaskDao std=new SyTaskDao(dbcon);
		std.queryOneRecord(st);
		Date beginTime=st.getBeginTime();
		Date endTime=st.getEndTime();
		final int len=(int)((endTime.getTime()-beginTime.getTime())/18000000)+2;
		System.out.println(len);
		String[] SubmitTime=new String[len];
		Calendar calendar = Calendar. getInstance();
		calendar.setTime(beginTime);
		for(int i=1;i<len;i++)
		{
			calendar.add(Calendar.HOUR_OF_DAY, 5);
			SubmitTime[i]=new SimpleDateFormat("yyyy-MM-dd hh").format(calendar.getTime());
		}
		return SubmitTime;
	}
	
	
	
	public static String[] getAllSupportBasicInfoName() {
		String[] ss = {"id",
				"email",
				"tel",
				"idcard",
				"college",
				"grade",
				"clas",
				"gender",
				"name",
				"nickname",
				"signature"};
		return ss;
	}
	
	// 获取 用户 所有进行中的任务
	public static SyTask[] getAllThingTask(String userId) { // no
		SyTask st=new SyTask();
		SyTaskSubmit sts=new SyTaskSubmit();
		DatabaseConnection dbcn=new DatabaseConnection();
		SyTaskSubmitDao stsd=new SyTaskSubmitDao(dbcn);
		SyTaskDao std=new SyTaskDao(dbcn);
		sts.setUserId(userId);
		SyTaskSubmit[] getsts=stsd.getAllByUserIdStatus(userId); //添加修改为queryAllRecordByStatusUserId
		SyTask[] sy=new SyTask[getsts.length];
		for(int i=0;i<getsts.length;i++)
		{
			st.setTaskId(getsts[i].getTaskId());
			std.queryOneRecord(st);
			sy[i]=st;
		}
		dbcn.close();
		return sy;
	}
	
	// 获取 用户 所有 已发布的任务
	public static SyTask[] getAllPushTask(String userId) { // no
		DatabaseConnection dbcn=new DatabaseConnection();
		SyTaskDao std=new SyTaskDao(dbcn);
		SyTask[] sy = std.queryAllRecodByUserId(userId);
		dbcn.close();
		return sy;
	}
	
	// 获取 一个唯一的 6位 TaskId
	public static String getOnlyTaskId() { // no
		DatabaseConnection dbcon = new DatabaseConnection();
		SyTaskDao std=new SyTaskDao(dbcon);
		String taskId=StringTool.getRandomCapitalString(6);
		while(std.checkOneRecord(taskId)) {  // Id 存在
			taskId=StringTool.getRandomCapitalString(6);
		}
		dbcon.close();
		return taskId;
	}
	
	public static String[] getBasicInfoName(String taskId) { // no
		DatabaseConnection dbcon=new DatabaseConnection();
		SyTaskDao std=new SyTaskDao(dbcon);
		SyTask st=new SyTask();
		st.setTaskId(taskId);
		if(std.queryOneRecord(st)) {
			String basciInfo=st.getBasicInfo();
		
			dbcon.close();
			String strings[]=StringTool.toManyString(basciInfo);
			//System.out.println(strings[1]);
			return strings;
		}
		dbcon.close();
		return new String[0];
	}
	
	public static String[] getExtraInfoName(String taskId) { // no
		DatabaseConnection dbcon=new DatabaseConnection();
		SyTaskDao std=new SyTaskDao(dbcon);
		SyTask st=new SyTask();
		st.setTaskId(taskId);
		if(std.queryOneRecord(st)) {
			String basciInfo=st.getExtraInfo();
			dbcon.close();
			String strings[]=StringTool.toManyString(basciInfo);
			//System.out.println(strings[1]);
			return strings;
		}
		dbcon.close();
		return new String[0];
	}
	
	public static String[] getBasicInfo(String userId, String taskId) { //yes
		String[] basciInfoName=getBasicInfoName(taskId);
		String[] basciInfo = new String[basciInfoName.length];
		UserData ud=new UserData();
		ud.setId(userId);

		UserDataDao udd=new UserDataDao();
		if (!udd.queryOneRecord(ud)) {
			return new String[0];
		}
		for (int i=0; i<basciInfoName.length; i++) {   //根据基本信息要求获取信息
			if ("id".equals(basciInfoName[i]))
				basciInfo[i] = ud.getId();
			else if ("email".equals(basciInfoName[i]))
				basciInfo[i] = ud.getEmail();
			else if ("tel".equals(basciInfoName[i]))
				basciInfo[i] = ud.getTel();
			else if ("idcard".equals(basciInfoName[i]))
				basciInfo[i] = ud.getIdcard();
			else if ("grade".equals(basciInfoName[i]))
				basciInfo[i] = ud.getGrade();
			else if ("college".equals(basciInfoName[i]))
				basciInfo[i] = ud.getCollege();
			else if ("clas".equals(basciInfoName[i]))
				basciInfo[i] = ud.getClas();
			else if ("registrationTime".equals(basciInfoName[i]))
				basciInfo[i] = ud.getRegistrationTime();
			else if ("lastIp".equals(basciInfoName[i]))
				basciInfo[i] = ud.getLastIP();
		}
		return basciInfo;
	}
	public static String getExcelPath(String userId, String taskId) {
		return WebPathTool.getTaskDataPath()+ userId + File.separator + taskId + File.separator
				+ "infomation.xls";
	}
	public static String getTreasuryPath(String userId, String taskId) {
		return WebPathTool.getTaskDataPath() + userId + File.separator
				+ taskId
				+ "/treasury/";
	}
	public static void main(String args[]) {
		//TaskService.getSubmitTime("ISCNYT");
	}
}
